package pearson.automation.forms.courseDetails;

import org.openqa.selenium.By;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.ComboBox;
import framework.elements.TextBox;

/**
 * Form for collaborators adding. Location:
 * CourseForm->Collaborators->Add
 */
public class AddCollaborators extends BaseForm {

    private static TextBox filter = new TextBox(
            By.id("find_collaborator"), "Collaborator search filter");
    private Button submit = new Button(
            By.cssSelector("input[value='Send Request']"), "Send Request");
    private final String addXpath =
        "//div[@data-collaborator-ref][contains(.,'%1$s')]"
      + "/div/div[contains(.,'Add')]";
    private final String rightsXpath =
        "//div[@data-collaborator-ref][contains(.,'%1$s')]"
      + "/select[@class='select-right']";

    public enum AccessRights {
        RO("read"), RW("read_write"), CREATOR("creator");

        private String val;

        AccessRights(final String str) {
            val = str;
        }

        public String toString() {
            return val;
        };
    }

    public AddCollaborators() {
        super(filter.getLocator(), "Add collaborator");
    }

    /**
     * Adding collaborator to course. Note: collaboratorName should be unique.
     * @param collaboratorName
     *            Collaborator's name
     * @param right
     *            - ReadOnly or ReadWrite
     * @return collaborators form
     */
    public final CollaboratorsForm addCollaborator(
            final String collaboratorName,
            final AccessRights right) {
        filter.type(collaboratorName);
        Button add = new Button(
                By.xpath(String.format(addXpath, collaboratorName)), "Add");
        add.click();
        ComboBox rights = new ComboBox(
                By.xpath(String.format(rightsXpath, collaboratorName)),
                "Access Rights");
        rights.selectByValue(right.toString());
        submit.clickAndWait();
        return new CollaboratorsForm();
    }
}
